package com.yunxin.core.wechat.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.core.admin.domain.AdminUser;
import com.yunxin.core.admin.service.AdminUserService;
import com.yunxin.core.wechat.domain.WechatInfo;
import com.yunxin.core.wechat.domain.WechatUserSys;
import com.yunxin.core.wechat.service.WechatInfoService;
import com.yunxin.core.wechat.service.WechatUserSysService;
import com.yunxin.core.wechat.mapper.WechatUserSysMapper;
import com.yunxin.core.wechat.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* @author Administrator
* @description 针对表【wechat_user_sys(企微用户id关联系统用户id)】的数据库操作Service实现
* @createDate 2024-05-29 11:15:35
*/
@Slf4j
@DS("crm")
@Service
public class WechatUserSysServiceImpl extends ServiceImpl<WechatUserSysMapper, WechatUserSys>
    implements WechatUserSysService{

    @Autowired
    private WechatInfoService wechatInfoService;
    @Autowired
    private AdminUserService adminUserService;


    @Override
    public AdminUser getAdminUser(LoginVo loginVo) {
        LambdaQueryWrapper<WechatInfo> wechatInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        wechatInfoLambdaQueryWrapper.eq(WechatInfo::getCorpid,loginVo.getCorpid());
        WechatInfo wechatInfo = wechatInfoService.getOne(wechatInfoLambdaQueryWrapper);
        if (ObjectUtils.isEmpty(wechatInfo)){
            return null;
        }
        LambdaQueryWrapper<WechatUserSys> wechatUserSysLambdaQueryWrapper=new LambdaQueryWrapper<>();
        wechatUserSysLambdaQueryWrapper.eq(WechatUserSys::getWechatInfoId,wechatInfo.getId())
                .eq(WechatUserSys::getWechatUserId,loginVo.getUserid());
        WechatUserSys wechatUserSys = this.getOne(wechatUserSysLambdaQueryWrapper);
        if (ObjectUtils.isEmpty(wechatUserSys)){
            return null;
        }
        return adminUserService.getById(wechatUserSys.getSysUserId());
    }
}




